<?php

namespace app\api\controller\yqfff;

use app\admin\model\FffAcc;
use think\cache\driver\Redis;
use think\Request;
use app\api\controller\Base;

/**
 * @author zero
 * @Time 2019/2/12
 *
 * Class BaseFff
 * @package app\api\controller\yqfff
 *
 * 一起飞飞飞基类
 */
class BaseFff extends Base
{

    public $add_coin_award = 1000; // 增加金币数
    public $add_diamond_award = 100; // 增加钻石数
    public $get_coin_resource_num = 5; // 获取金币次数上限
    public $get_diamond_resource_num = 5; // 获取钻石次数上限

    // 用户实例
    protected $acc;

    // 审核控制版本号
    protected $control_version = '0.0.0';

    // 当前版本号
    protected $version;

    // 首页展示的跳转其它小程序
    protected $important_list = array(
        array(
            'name' => '萌消团',
            'appid' => 'wxd26399f7e0a403d0',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mxt.png'
        ),
        array(
            'name' => '一起来合体',
            'appid' => 'wxd382be58d8c6d6c9',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/heti.png'
        ),
        array(
            'name' => '切水果勇士',
            'appid' => 'wxaa2853914381ec98',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/fruit.jpg'
        ),
        array(
            'name' => '拯救查尔斯',
            'appid' => 'wxc76853fdcf188c3e',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/charles.jpg'
        )
    );

    // 构造方法
    public function __construct(Request $request = null)
    {
        header('Access-Control-Allow-Origin:*'); // 跨域
        parent::__construct($request);
        $this->version = $request->post('version') ?: '1.0.0';
        $this->_initialize();
    }

    // 初始化操作
    protected function _initialize()
    {
        //移除HTML标签
        $this->request->filter('strip_tags');
        $actionname = strtolower($this->request->action());
        $token = $this->request->server('HTTP_TOKEN', $this->request->request('token')) ?: '3ab0f3dee498e487c416213ac0c09885';
        $this->acc = FffAcc::get(['token' => $token]);
        // 检测是否需要验证登录
        if ($actionname !== 'login') {
            //检测是否登录
            if (!$this->acc) {
                $this->result(__('Please login first'), null, 401);
            }
        }
    }

    // 获取当前用户增加资源剩余次数
    protected function get_add_resource_num()
    {
        $redis = new Redis();
        $get_coin_num = $this->get_coin_resource_num - $redis->get($this->acc->openid . '_fff_get_coin_num', 0);
        $get_diamond_num = $this->get_diamond_resource_num - $redis->get($this->acc->openid . '_fff_get_diamond_num', 0);
        $data['add_coin'] = array(
            'num' => $get_coin_num,
            'award' => $this->add_coin_award,
        );
        $data['add_diamond'] = array(
            'num' => $get_diamond_num,
            'award' => $this->add_diamond_award,
        );
        return $data;
    }

    // 处理条件是否完成
    public function handle_condition($str)
    {
        $arr = explode('_', $str);
        $condition = intval($arr[0]);
        $num = intval($arr[1]);
        $now_num = 0;
        switch ($condition) {
            case 1:
                $now_num = $this->acc->game_num;
                break;
            case 2:
                $now_num = $this->acc->resurrect_num;
                break;
            case 3:
                $now_num = $this->acc->block_num;
                break;
            case 4:
                $now_num = $this->acc->share_num;
                break;
            case 5:
                $now_num = $this->acc->login_day_num;
                break;
            case 6:
                $now_num = $this->acc->max_score;
                break;
            case 7:
                $now_num = $this->acc->all_score;
                break;
            case 8:
                $now_num = $this->acc->live_second;
                break;
            case 9:
                $now_num = $this->acc->share_user_num;
                break;
        }
        if ($now_num >= $num) {
            return true;
        } else {
            return false;
        }
    }

}
